var winW = 0;// 屏幕宽度
var winH = 0;// 屏幕高度

// 打印机设置
function printerSetup() {
	if(typeof  jsPrintSetup=='undefined'){
		$("body").html("该功能只适用于firefox，请安装firefox，且为firefox安装jsprintsetup插件");		
		return;
	}
	
	// alert(jsPrintSetup.getPrintersList());
	var printer_list = jsPrintSetup.getPrintersList();
	var printers = printer_list.split(",");

	for (var a in printers) {
		var html = "<div>" + printers[a] + ":";
		html += "<select id='" + printers[a] + "' class='printer'>";

		var papers = ['A4', '10x15', '10x10', '5x3', '3x2'];
		var paper = $.cookie(printers[a]);
		for (var b in papers) {
			html += "<option value='" + papers[b] + "' "
					+ (paper == papers[b] ? "selected" : "") + ">" + papers[b]
					+ "</option>";
		}

		html += "</select>";
		html += "</div>";
		$("#printerWrap").append(html);
	}
}
// 保存打印机设置
function savePrinter() {
	$(".printer").each(function() {
				var printer = this.id;
				var val = $(this).val();
				$.cookie(printer, val, {
							expires : 3600
						});
			});
	$.cookie("allPrinterOk", "1");// 打印机设置成功
	window.location.href = window.location.href;
}
// 获取打印机
function getPrinter(type) {
	var printer_list = jsPrintSetup.getPrintersList();
	var printers = printer_list.split(",");

	for (var a in printers) {
		if ($.cookie(printers[a]) == type) {
			return printers[a];
		}
	}
	return null;
}
// 自动打印
function jsprint(type) {
	if ($.cookie("allPrinterOk") != "1") {
		alert("未设置打印机");
		return;
	}
	var printer = getPrinter(type);//alert(type);
	if (!printer) {
		alert("未设置对应纸张的打印机");
		return;
	}
	// alert(printer);
	// return;
	jsPrintSetup.setPrinter(printer);
	jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);
	// set top margins in millimeters
	jsPrintSetup.setOption('marginTop', 0);
	jsPrintSetup.setOption('marginBottom', 0);
	jsPrintSetup.setOption('marginLeft', 0);
	jsPrintSetup.setOption('marginRight', 0);
	// set page header
	jsPrintSetup.setOption('headerStrLeft', '');
	jsPrintSetup.setOption('headerStrCenter', '');
	jsPrintSetup.setOption('headerStrRight', '');
	// set empty page footer
	jsPrintSetup.setOption('footerStrLeft', '');
	jsPrintSetup.setOption('footerStrCenter', '');
	jsPrintSetup.setOption('footerStrRight', '');
	// clears user preferences always silent print value
	// to enable using 'printSilent' option
	jsPrintSetup.clearSilentPrint();
	// Suppress print dialog (for this context only)
	jsPrintSetup.setOption('printSilent', 1);// 静默打印
	// Do Print
	// When print is submitted it is executed asynchronous and
	// script flow continues after print independently of completetion of print
	// process!
	jsPrintSetup.print();
	setTimeout(function() {
				self.close();// 关闭当前的window.open
			}, 500);
}

// paperType:纸张类型
function getWidthHeight(paperType) {
	var papers = ['A4', '10x15', '10x10', '5x3', '3x2'];
	switch (paperType) {
		case 'A4' :
			var param = [960, 768];
			break;
		default :
			var param = [400, 400];
	}
	return param;
}
// 自动打印 paperType:纸张类型
function openPrint(html, paperType) {
	var param = getWidthHeight(paperType);

	var width = param[0];
	var height = param[1];

	var top = (winH - height - 100) / 2;
	var left = (winW - width) / 2;

	var pwin = window.open("jsprint.html", "jsprint", 'height=' + height
					+ ', width=' + width + ', top=' + top + ', left=' + left
					+ ', toolbar=no, menubar=no, scrollbars=yes,resizable=yes');
	pwin.document.write(html);
	pwin.document.close(); // 这句很重要哦
}


//get方式 paperType:纸张类型
function dialogPrint(url, paperType) {
	var param = getWidthHeight(paperType);

	var width = param[0];
	var height = param[1];
	
	var top = (winH - height - 100) / 2;
	var left = (winW - width) / 2;
	//alert(left+" "+winW +" "+ width);
	window.showModalDialog(url,window,"dialogHeight:"+height+"px;dialogWidth:"+width+"px;dialogLeft:"+left+"px;dialogTop:"+top+"px;resizable:no;status:no;scroll:yes;help:no"); 

}

$(function() {
	winW = window.screen.availWidth;// 初始化
	winH = window.screen.availHeight;// 初始化
		// printerSetup();
	});
